package NIO;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

public class NioTest {
    @Test
    public void TestBuffer(){
        IntBuffer buffer = IntBuffer.allocate(10);
        buffer.put(new int[]{1,2,3,4,5});
        buffer.flip();
        for (int i = 0; i < 5; i++) {
            System.out.println(buffer.get());
        }
    }

    @Test
    public void TestChannel() throws IOException {
       FileOutputStream fileOutputStream = new FileOutputStream("D:/a.txt");
        FileChannel channel = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.wrap("TestChannel2".getBytes());
//        byteBuffer.put("TestChannel".getBytes());
//        byteBuffer.flip();
        channel.write(byteBuffer);
        channel.close();
        fileOutputStream.close();

    }

}
